<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body>
<div id="app">
    <h2>用户登录</h2>
    <form @submit.prevent="login">
        <label>账号: <input type="text" v-model="username"></label><br><br>
        <label>密码: <input type="password" v-model="password"></label><br><br>
        <button type="submit">登录</button>
    </form>
    <p style="color: red;">{{ errorMessage }}</p>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            username: '',
            password: '',
            errorMessage: ''
        },
        methods: {
            login() {
                // 发送 POST 请求到后端
                axios.post('http://localhost:8080/login', {
                    username: this.username,
                    password: this.password
                }, {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then(response => {
                    // 如果登录成功
                    if (response.data.code === 200) {
                        window.location.href = 'students.html'; // 跳转到学生页面
                    } else {
                        this.errorMessage = '账号或密码错误';
                    }
                }).catch(error => {
                    console.error(error);
                    this.errorMessage = '登录失败，请重试';
                });
            }
        }
    });
</script>
</body>
</html>
